﻿using System;
using System.Collections.Generic;
using System.Text;
using TSVNAddin.SVN;

namespace TSVNAddin.Core
{
	public class WorkingRootFinder
	{
		private readonly SVNClientAdapter _SVNClientAdapter;

		public WorkingRootFinder(SVNClientAdapter svnClientAdapter)
		{
			_SVNClientAdapter = svnClientAdapter;
		}

		public string FindRoot(string path)
		{
			string directory = System.IO.Path.GetDirectoryName(path);
            string workingRoot = string.Empty;
			
            while(true)
			{
                string formattedDirectory = directory + "\\";

                if (_SVNClientAdapter.IsWorkingCopy(formattedDirectory))
                    workingRoot = formattedDirectory;

				int indexSeparator = directory.LastIndexOf('\\');
				if (indexSeparator == -1)
					return workingRoot;

				directory = directory.Remove(indexSeparator);
			}
		}
	}
}
